﻿using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


namespace CRM.Utility.Logging
{
    /// <summary>
    /// 原生log4net
    /// </summary>
    public static class Log4netLog
    {
        static Log4netLog()
        {
            //var dirPath = AppDomain.CurrentDomain.BaseDirectory;
            //if (System.Web.HttpContext.Current != null)
            //{
            //    dirPath = RequestUtility.GetMapPathString();
            //}
            //var configPath = Path.Combine(dirPath, "configs\\log4Net.config");
            //XmlConfigurator.Configure(new FileInfo(configPath));
            //应用程序启动时，自动加载配置log4Net  
            XmlConfigurator.Configure();
        }

        #region 错误日志方法

        /// <summary>
        /// 错误日志日志
        /// </summary>
        /// <param name="errorLog"></param>
        public static void Error(ErrorLog errorLog)
        {
            ILog logger = LogManager.GetLogger("ErrorLogAdoNetAppenderLogger");
            if (logger.IsErrorEnabled)
            {
                logger.Error(errorLog);
            }
        }

        /// <summary>
        /// 错误日志日志
        /// </summary>
        /// <param name="ex"></param>
        public static void Error(Exception ex)
        {
            ILog logger = LogManager.GetLogger("ErrorLogAdoNetAppenderLogger");
            ErrorLog message = new ErrorLog
            {
                Platform = 1,
                CreatedDate = DateTime.Now,
                ErrorSource = ex.Source,
                ErrorTrace = ex.StackTrace,
                ErrorMessage = ex.Message,
                ClientIP = IPHelper.IPAddress,
                RequestUrl = RequestUtility.GetUrl(),
                RequestMethod = RequestUtility.GetRequesType()
            };
            if (logger.IsErrorEnabled)
            {
                logger.Error(message);
            }
        }

        public static void Error(int platFormID,Exception ex)
        {
            ILog logger = LogManager.GetLogger("ErrorLogAdoNetAppenderLogger");
            ErrorLog message = new ErrorLog
            {
                Platform = platFormID,
                CreatedDate = DateTime.Now,
                ErrorSource = ex.Source,
                ErrorTrace = ex.StackTrace,
                ErrorMessage = ex.Message,
                ClientIP = IPHelper.IPAddress,
                RequestUrl = RequestUtility.GetUrl(),
                RequestMethod = RequestUtility.GetRequesType()
            };
            if (logger.IsErrorEnabled)
            {
                logger.Error(message);
            }
        }

        public static void Error(string errorMsg)
        {
            ILog logger = LogManager.GetLogger("ErrorLogAdoNetAppenderLogger");
            ErrorLog message = new ErrorLog()
            {
                Platform = 0,
                CreatedDate = DateTime.Now,
                ErrorMessage = errorMsg,
                ClientIP = IPHelper.IPAddress,
                RequestUrl = RequestUtility.GetUrl(),
                RequestMethod = RequestUtility.GetRequesType()
            };
            if (logger.IsErrorEnabled)
            {
                logger.Error(message);
            }

        }
        public static void Error(int platFormID,string errorMsg)
        {
            ILog logger = LogManager.GetLogger("ErrorLogAdoNetAppenderLogger");
            ErrorLog message = new ErrorLog()
            {
                Platform = platFormID,
                CreatedDate = DateTime.Now,
                ErrorMessage = errorMsg,
                ClientIP = IPHelper.IPAddress,
                RequestUrl = RequestUtility.GetUrl(),
                RequestMethod = RequestUtility.GetRequesType()
            };
            if (logger.IsErrorEnabled)
            {
                logger.Error(message);
            }

        }

        #endregion

        #region  调试日志方法

        /// <summary>
        /// 文件日志
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(string message)
        {
            ILog logger = LogManager.GetLogger("DebugLogTxtAppenderLogger");
            logger.Debug(message);
            //var log = new ActionLog()
            //{
            //    CreatedDate = DateTime.Now,
            //    ClientIP = IPHelper.GetUserIp(),
            //    RequestUrl = RequestUtility.GetUrl(),
            //    RequestMethod = RequestUtility.GetRequesType(),
            //    PostData = message
            //};
            //Debug(log);
        }


        #endregion

        #region info日志方法
        public static void Info(string message)
        {
            var log = new ActionLog()
            {
                CreatedDate = DateTime.Now,
                ClientIP = IPHelper.IPAddress,
                RequestUrl = RequestUtility.GetUrl(),
                RequestMethod = RequestUtility.GetRequesType(),
                PostData = message
            };
            Info(log);

        }

        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        public static void Info<T>(T data)
        {
            var log = new ActionLog()
            {
                CreatedDate = DateTime.Now,
                ClientIP = IPHelper.IPAddress,
                RequestUrl = RequestUtility.GetUrl(),
                RequestMethod = RequestUtility.GetRequesType(),
                PostData = Newtonsoft.Json.JsonConvert.SerializeObject(data)
            };
            Info(log);
        }
        public static void Info(ActionLog accessInfo)
        {
            ILog logger = LogManager.GetLogger("ActionLogAdoAppenderLogger");
            if (logger.IsInfoEnabled)
                logger.Info(accessInfo);
        }
        #endregion
    }
}
